package com.zwr.sheller.customer.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.KQRBusinessException;
import com.zwr.sheller.core.common.KQRExceptionCode;
import com.zwr.sheller.core.pojo.Address;
import com.zwr.sheller.core.util.RegexUtil;
import com.zwr.sheller.customer.Service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *
 *@author Kang
 *@date
 */
@RestController
@RequestMapping("/shippingAddressController")
@Api(value = "收货地址管理")
public class ShippingAddressController {

    private final Long id=1L;
    @Autowired
    AddressService addressService;
    @ApiOperation(value = "增加收货地址")
    @PostMapping("/addShippingAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressName", dataType = "String", required = true, paramType = "query", value = "收货人姓名"),
            @ApiImplicitParam(name = "addressPhone", dataType = "String", required = true, paramType = "query", value = "收货人手机号"),
            @ApiImplicitParam(name = "addressArea", dataType = "String", required = true, paramType = "query", value = "收货地址（省、市）"),
            @ApiImplicitParam(name = "addressDetail", dataType = "String", required = true, paramType = "query", value = "详细地址"),
            @ApiImplicitParam(name = "sort", dataType = "String", required = false, paramType = "query", value = "邮编")})
       public Map<String,Object> addShippingAddress(String addressName,String addressPhone,String addressArea,String addressDetail,String sort) throws KQRBusinessException {
        Address address=new Address();
        address.setAddressName(addressName);
        if(  RegexUtil.isPhoneNumber(addressPhone)){
            address.setAddressPhone(addressPhone);
        }else {
            throw new KQRBusinessException(KQRExceptionCode.UNKNOWN_EXCEPTION);
        }
        address.setAddressArea(addressArea);
        address.setAddressDetail(addressDetail);
        address.setAddressPostCode(sort);
        address.setUserId(1L);
        Integer add = addressService.add(address);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "修改地址")
    @PostMapping("/updateShippingAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressName", dataType = "String", required = true, paramType = "query", value = "收货人姓名"),
            @ApiImplicitParam(name = "addressPhone", dataType = "String", required = true, paramType = "query", value = "收货人手机号"),
            @ApiImplicitParam(name = "addressArea", dataType = "String", required = true, paramType = "query", value = "收货地址（省、市）"),
            @ApiImplicitParam(name = "addressDetail", dataType = "String", required = true, paramType = "query", value = "详细地址"),
            @ApiImplicitParam(name = "sort", dataType = "String", required = false, paramType = "query", value = "邮编")})
    public Map<String,Object> updateShippingAddress(String addressName,String addressPhone,String addressArea,String addressDetail,String sort) throws KQRBusinessException {
        Address address=new Address();
        address.setAddressName(addressName);

        if(  RegexUtil.isPhoneNumber(addressPhone)){
            address.setAddressPhone(addressPhone);
        }else {
            throw new KQRBusinessException(KQRExceptionCode.UNKNOWN_EXCEPTION);
        }



        address.setAddressArea(addressArea);
        address.setAddressDetail(addressDetail);
        address.setAddressPostCode(sort);
        address.setUserId(id);
        Integer add = addressService.update(address);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "删除收货地址")
    @PostMapping("/deleteShippingAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "Long", required = true, paramType = "query", value = "收货地址id")
    })
    public Map<String,Object> deleteShippingAddress(Long id){

        Integer delete = addressService.delete(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "展示收货地址")
    @PostMapping("/selectShippingAddress")
    public Map<String,Object> selectShippingAddress(){
        Map<String, Object> map = addressService.selectAll(1L);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",map);
        return jsonResult.getMap();
    }
}
